package comic;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import android.graphics.drawable.Drawable;

public class GunnerkriggCourt extends Comic {

	private static final long serialVersionUID = -7456043043790439344L;

	@Override
	public String get_display_name() {
		return "Gunnerkrigg Court";
	}


	@Override
	public int get_first_comic_n() {
		return 1;
	}


	@Override
	public int getLatest() throws MalformedURLException, IOException {
		String content = get_website_content("http://www.gunnerkrigg.com/index2.php");
		String result = content.replaceAll(".*http://www.gunnerkrigg.com//comics/(\\d+).jpg.*", "$1");

		return Integer.parseInt(result);
	}

	@Override
	public Strip getStrip(int id) throws MalformedURLException, IOException {
		int latest=getLatest();
		Strip s=new Strip();
		
		if(id<first_comic)
			id=first_comic;
		else if (id>latest)
			id=latest;
		s.id=id;
		
		
		String url = String.format("http://www.gunnerkrigg.com/archive_page.php?comicID=%d", id);
		String content = get_website_content(url);
		
		String regex = ".*alt=\"(.*?)\" border=\"0\" />.*";
		s.title = content.replaceAll(regex, "$1");
		
		URL image_url = new URL(String.format("http://www.gunnerkrigg.com//comics/%08d.jpg", id));
		s.image=Drawable.createFromStream(image_url.openStream(), "");
		
		regex = ".*<p/><b></b>\\s*<br/>(.*?)<.*";
		s.text = content.replaceAll(regex, "$1");
		
		return s;
	}
}
